/**
 * 
 * 起迪科技 Copyright (c) 2014-2018 QiDi,Inc.All Rights Reserved.
 */
package cn.qidisoft.edu.hzjt.service.impl;

import java.io.File;
import java.util.List;

import org.apache.commons.io.FileUtils;
import org.apache.commons.lang3.builder.ToStringBuilder;
import org.apache.commons.lang3.builder.ToStringStyle;
import org.apache.ibatis.session.RowBounds;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.qidisoft.core.common.code.QdfRespCode;
import cn.qidisoft.core.common.context.ApplicationContextConfigUtil;
import cn.qidisoft.core.common.exception.dao.DBException;
import cn.qidisoft.core.common.log.LoggerAdapter;
import cn.qidisoft.core.common.log.LoggerAdapterFactory;
import cn.qidisoft.core.common.pagination.Page;
import cn.qidisoft.core.common.pagination.PaginationBean;
import cn.qidisoft.core.util.uuid.UUIDGenerator;
import cn.qidisoft.edu.hzjt.dao.TwglStudentCadreAttMapper;
import cn.qidisoft.edu.hzjt.dao.TwglStudentCadreMapper;
import cn.qidisoft.edu.hzjt.model.TwglStudentCadre;
import cn.qidisoft.edu.hzjt.model.TwglStudentCadreAtt;
import cn.qidisoft.edu.hzjt.service.ITwglStudentCadreService;
import cn.qidisoft.edu.hzjt.utils.LogType;

/**
 * 
 * 
 * @author Administrator
 * @version $Id: BjxxClassMaterialProjectServiceImpl.java, v 0.1 2018年10月29日 下午1:14:36 hhl Exp $
 */
@Service("twglStudentCadreService")
public class TwglStudentCadreServiceImpl implements ITwglStudentCadreService {

    protected static final LoggerAdapter LOGGER = LoggerAdapterFactory
        .getLogger(LogType.HZJT_LOGGER.val);

    @Autowired
    private TwglStudentCadreMapper       twglStudentCadreMapper;

    @Autowired
    private TwglStudentCadreAttMapper    twglStudentCadreAttMapper;

    /** 
     * @see cn.qidisoft.microClass.resource.service.ICatalogService#queryList(cn.qidisoft.microClass.resource.model.basis.Catalog)
     */
    @Override
    public List<TwglStudentCadre> queryList(TwglStudentCadre data) {
        try {
            return twglStudentCadreMapper.queryList(data);

        } catch (Exception e) {
            LOGGER.error("queryList [{}] error," + this.getClass().getName() + "=[{}]",
                this.getClass().getName(),
                ToStringBuilder.reflectionToString(data, ToStringStyle.SHORT_PREFIX_STYLE));
            throw new DBException(QdfRespCode.QDF_020002.getCode(), QdfRespCode.QDF_020002.getMsg(),
                e);
        }
    }

    /** 
     * @see cn.qidisoft.microClass.resource.service.ICatalogService#queryByPK(cn.qidisoft.microClass.resource.model.basis.Catalog)
     */
    @Override
    public TwglStudentCadre queryByPK(TwglStudentCadre data) {
        try {
            return twglStudentCadreMapper.queryByPK(data);
        } catch (Exception e) {
            LOGGER.error("queryByPK [{}] error," + this.getClass().getName() + "=[{}]",
                this.getClass().getName(),
                ToStringBuilder.reflectionToString(data, ToStringStyle.SHORT_PREFIX_STYLE));
            throw new DBException(QdfRespCode.QDF_020002.getCode(), QdfRespCode.QDF_020002.getMsg(),
                e);
        }
    }

    /** 
     * @see cn.qidisoft.microClass.resource.service.ICatalogService#add(cn.qidisoft.microClass.resource.model.basis.Catalog)
     */
    @Override
    @Transactional
    public String add(TwglStudentCadre data) {
        String uuid = UUIDGenerator.genReqID();
        try {
            data.setGid(uuid);
            twglStudentCadreMapper.add(data);
        } catch (Exception e) {
            LOGGER.error("add [{}] error," + this.getClass().getName() + "=[{}]",
                this.getClass().getName(),
                ToStringBuilder.reflectionToString(data, ToStringStyle.SHORT_PREFIX_STYLE));
            throw new DBException(QdfRespCode.QDF_020002.getCode(), QdfRespCode.QDF_020002.getMsg(),
                e);
        }
        return uuid;
    }

    /** 
     * @see cn.qidisoft.microClass.resource.service.ICatalogService#update(cn.qidisoft.microClass.resource.model.basis.Catalog)
     */
    @Override
    @Transactional
    public void update(TwglStudentCadre data) {
        try {
            twglStudentCadreMapper.update(data);
        } catch (Exception e) {
            LOGGER.error("update [{}] error," + this.getClass().getName() + "=[{}]",
                this.getClass().getName(),
                ToStringBuilder.reflectionToString(data, ToStringStyle.SHORT_PREFIX_STYLE));
            throw new DBException(QdfRespCode.QDF_020002.getCode(), QdfRespCode.QDF_020002.getMsg(),
                e);
        }
    }

    /** 
     * @see cn.qidisoft.microClass.resource.service.ICatalogService#delete(cn.qidisoft.microClass.resource.model.basis.Catalog)
     */
    @Override
    @Transactional
    public void delete(TwglStudentCadre data) {
        try {

            for (String id : data.getIds()) {
                TwglStudentCadreAtt twglStudentCadreAtt = new TwglStudentCadreAtt();
                twglStudentCadreAtt.setParentid(Integer.valueOf(id));
                List<TwglStudentCadreAtt> twglStudentCadreAttList = twglStudentCadreAttMapper
                    .queryList(twglStudentCadreAtt);

                if (twglStudentCadreAttList != null && !twglStudentCadreAttList.isEmpty()) {
                    for (TwglStudentCadreAtt r : twglStudentCadreAttList) {
                        String upload = ApplicationContextConfigUtil.getString("file.path", null);
                        twglStudentCadreAtt.setGid(r.getGid());
                        twglStudentCadreAttMapper.delete(twglStudentCadreAtt);
                        File file = new File(upload + File.separator + r.getGid());
                        if (file.isDirectory()) {//判断file是否是文件目录 若是返回TRUE  
                            String name[] = file.list();//name存储file文件夹中的文件名  
                            for (int i = 0; i < name.length; i++) {
                                File f = new File(upload + File.separator + r.getGid(), name[i]);//此时就可得到文件夹中的文件  
                                f.delete();//删除文件  
                            }
                            FileUtils.forceDelete(new File(upload + File.separator + r.getGid())); //最后删除文件目录
                        }
                    }

                }
            }
            twglStudentCadreMapper.delete(data);
        } catch (Exception e) {
            LOGGER.error("delete [{}] error," + this.getClass().getName() + "=[{}]",
                this.getClass().getName(),
                ToStringBuilder.reflectionToString(data, ToStringStyle.SHORT_PREFIX_STYLE));
            throw new DBException(QdfRespCode.QDF_020002.getCode(), QdfRespCode.QDF_020002.getMsg(),
                e);
        }
    }

    /** 
     * @see cn.qidisoft.microClass.resource.service.ICatalogService#queryPageList(cn.qidisoft.microClass.resource.model.basis.Catalog, cn.qidisoft.core.common.pagination.Page)
     */
    @Override
    public PaginationBean<TwglStudentCadre> queryPageList(TwglStudentCadre data, Page page) {
        try {

            int c = twglStudentCadreMapper.queryPageListcount(data);
            int totalRecords = 0;
            totalRecords = (int) c;
            PaginationBean<TwglStudentCadre> pageHolder = new PaginationBean<TwglStudentCadre>(page,
                totalRecords);
            List<TwglStudentCadre> resultList = twglStudentCadreMapper.queryPageList(data,
                new RowBounds((pageHolder.getCurrentPage() - 1) * pageHolder.getPageSize(),
                    pageHolder.getPageSize()));
            pageHolder.setPageList(resultList);
            return pageHolder;
        } catch (Exception e) {
            LOGGER.error("queryPageList [{}] error," + this.getClass().getName() + "=[{}]",
                this.getClass().getName(),
                ToStringBuilder.reflectionToString(data, ToStringStyle.SHORT_PREFIX_STYLE));
            throw new DBException(QdfRespCode.QDF_020002.getCode(), QdfRespCode.QDF_020002.getMsg(),
                e);
        }
    }

    /** 
     * @see cn.qidisoft.microClass.resource.service.ICatalogService#validation(cn.qidisoft.microClass.resource.model.basis.Catalog)
     */
    @Override
    public int validation(TwglStudentCadre data) {
        try {
            return twglStudentCadreMapper.validation(data);
        } catch (Exception e) {
            e.printStackTrace();
            return -99;
        }
    }

    /** 
     * @see cn.qidisoft.edu.hzjt.service.ITwglStudentCadreService#queryPageList_admin(cn.qidisoft.edu.hzjt.model.TwglStudentCadre, cn.qidisoft.core.common.pagination.Page)
     */
    @Override
    public PaginationBean<TwglStudentCadre> queryPageList_admin(TwglStudentCadre data, Page page) {
        try {

            int c = twglStudentCadreMapper.queryPageListcount_admin(data);
            int totalRecords = 0;
            totalRecords = (int) c;
            PaginationBean<TwglStudentCadre> pageHolder = new PaginationBean<TwglStudentCadre>(page,
                totalRecords);
            List<TwglStudentCadre> resultList = twglStudentCadreMapper.queryPageList_admin(data,
                new RowBounds((pageHolder.getCurrentPage() - 1) * pageHolder.getPageSize(),
                    pageHolder.getPageSize()));
            pageHolder.setPageList(resultList);
            return pageHolder;
        } catch (Exception e) {
            LOGGER.error("queryPageList [{}] error," + this.getClass().getName() + "=[{}]",
                this.getClass().getName(),
                ToStringBuilder.reflectionToString(data, ToStringStyle.SHORT_PREFIX_STYLE));
            throw new DBException(QdfRespCode.QDF_020002.getCode(), QdfRespCode.QDF_020002.getMsg(),
                e);
        }
    }

    /** 
     * @see cn.qidisoft.edu.hzjt.service.ITwglStudentCadreService#auditList(cn.qidisoft.edu.hzjt.model.TwglStudentCadre)
     */
    @Override
    @Transactional
    public void auditList(TwglStudentCadre data) {
        try {
            for (String s : data.getIds()) {
                TwglStudentCadre data1 = new TwglStudentCadre();
                data1.setGbid(Integer.valueOf(s));
                data1.setStates(data.getStates());
                data1.setAuditTime(data.getAuditTime());
                data1.setAuditSuggestion(null);
                twglStudentCadreMapper.auditList(data1);
            }
        } catch (Exception e) {
            LOGGER.error("update [{}] error," + this.getClass().getName() + "=[{}]",
                this.getClass().getName(),
                ToStringBuilder.reflectionToString(data, ToStringStyle.SHORT_PREFIX_STYLE));
            throw new DBException(QdfRespCode.QDF_020002.getCode(), QdfRespCode.QDF_020002.getMsg(),
                e);
        }
    }

    /** 
     * @see cn.qidisoft.edu.hzjt.service.ITwglStudentCadreService#export(cn.qidisoft.edu.hzjt.model.TwglStudentCadre)
     */
    @Override
    public List<TwglStudentCadre> export(TwglStudentCadre data) {
        try {
            return twglStudentCadreMapper.export(data);
        } catch (Exception e) {
            LOGGER.error("export [{}] error," + this.getClass().getName() + "=[{}]",
                this.getClass().getName(),
                ToStringBuilder.reflectionToString(data, ToStringStyle.SHORT_PREFIX_STYLE));
            throw new DBException(QdfRespCode.QDF_020002.getCode(), QdfRespCode.QDF_020002.getMsg(),
                e);
        }
    }

    /** 
     * @see cn.qidisoft.edu.hzjt.service.ITwglStudentCadreService#queryPageList_bzr(cn.qidisoft.edu.hzjt.model.TwglStudentCadre, cn.qidisoft.core.common.pagination.Page)
     */
    @Override
    public PaginationBean<TwglStudentCadre> queryPageList_bzr(TwglStudentCadre data, Page page) {
        return null;
    }

    /** 
     * @see cn.qidisoft.edu.hzjt.service.ITwglStudentCadreService#queryByPK_bzr(cn.qidisoft.edu.hzjt.model.TwglStudentCadre)
     */
    @Override
    public TwglStudentCadre queryByPK_bzr(TwglStudentCadre data) {
        return null;
    }

}
